package com.yangsaiyu.dockerspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yangsaiyu.dockerspringboot.config.AjaxResult;
import com.yangsaiyu.dockerspringboot.entity.Student;
import com.yangsaiyu.dockerspringboot.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author yangsaiyu
 */
@RestController
@Slf4j
public class MyController {

    @Autowired
    private IStudentService studentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("test1")
    public String test1(){
        return "day12-27测试jenkins";
    }

    @RequestMapping("test2")
    public String test2(){
        return "jenkins-test2";
    }

    @RequestMapping("getAll")
    public AjaxResult test3(){
        log.info("进入getAll");
        List<Student> list = studentService.list();
        return AjaxResult.ok(list);
    }

    @GetMapping("getById")
    public AjaxResult test4(@RequestParam("id") Integer id){
        log.info("进入getById");
        if (redisTemplate.hasKey("student:"+id)){
            log.info("进入redis取数据");
            return AjaxResult.ok(redisTemplate.opsForValue().get("student:"+id));
        }
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("id",id);
        Student one1 = studentService.getOne(studentQueryWrapper);
        redisTemplate.opsForValue().set("student:"+id,one1,2, TimeUnit.MINUTES);
        return AjaxResult.ok(one1);
    }
}
